<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .drag {
          width: 100px;
          height: 100px;
          position: absolute;
          top: 0;
          left: 0;
          background-color: pink;
          border: 1px solid #ccc;
        }
      </style>
</head>
<body>
    <div id="app">
        <div ref="box" @click="box" class="drag"></div>
      </div>
</body>
<script src="./vue.js"></script>
<script src="./axios.js"></script>
<script src="./socket.io.js"></script>
<script>
    let socket = io.connect();
    new Vue({
        el:"#app",
        data:{

        },
        methods:{
            box(e){
              let  target=e.target;
            // window.onload=function(){
                console.log(1);
                target.onmousedown=function(e){
                let clientX=e.pageX - target.offsetLeft;
                let clientY=e.pageY - target.offsetTop;
                // console.log(clientX);
                // console.log(clientY);
                document.onmousemove=function(e){
                    target.style.left=e.pageX-clientX+"px";
                    target.style.top=e.pageY-clientY+"px";
                    let x = target.style.left;
                    let y = target.style.top;
                    let obj = {x:x,y:y}
                    socket.emit("sendXY",obj)
                }
                document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
                };
                }
            // }
            },
        },

        created: async function (e) {
            await socket.on("toxy",(res)=>{
                this.$refs.box.style.left=res.x;
                this.$refs.box.style.top=res.y;
                // e.target.left=res.x;
                // e.target.top=res.y;
            })
        }
    })
</script>
</html>